﻿#include "algosetform.h"
#include "ui_algosetform.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "algo_cam_l_form.h"
#include "algo_cam_h_form.h"
#include "algo_arm_form.h"
#include "algo_dzft_form.h"
#include "singleton.h"
#include <QMouseEvent>
#include <QMessageBox>
#include "myapp.h"

AlgoSetForm::AlgoSetForm(QString devType, QString chnid, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AlgoSetForm)
{
    ui->setupUi(this);
    m_devType = devType;
    m_chnid = chnid;
    this->initForm();
    this->setWindowFlags(Qt::FramelessWindowHint );
    show_alarmExtern = false;
    realmode = 0;
    // connect(Singleton::Instance(), &Singleton::sigSetAlarmInfoSuccess, this, [ = ]()
    // {
    //     if(ui->btn_RealTimeImage->text() == "关闭预览")
    //     {
    //         ui->btn_RealTimeImage->setText("打开预览");
    //         ui->widget_right->clear();
    //         QMessageBox::information(nullptr, "提示", "实时预览关闭成功");
    //     }
    //     else
    //     {
    //         ui->btn_RealTimeImage->setText("关闭预览");
    //     }
    // }, Qt::QueuedConnection);
//    this->onGetZoneParam(QJsonObject());

}

AlgoSetForm::~AlgoSetForm()
{
    delete ui;
}
void AlgoSetForm::initForm()
{

    if(m_devType == "AI中低速相机")
    {
        this->initCamLForm();
    }
    else if(m_devType == "AI高速相机")
    {
        this->initCamFForm();
    }
    else  if(m_devType == "分析装置")
    {
        this->initARMForm();
    }
    else if(m_devType == "GPU服务器")
    {
        this->initGPUForm();
    }
    else if(m_devType == "电子封条分析装置")
    {
        this->initDZFTForm();
    }

    ui->lab_theme->adjustSize();

    connect(Singleton::Instance(), &Singleton::sigGetbeltWidth, this, &AlgoSetForm::onGetBeltWidthParam);//2022.3.18

}

void AlgoSetForm::initCamLForm()
{
    ui->lab_theme->setText("中低速相机 -算法配置");
    Algo_Cam_L_Form *cam_l_form = new Algo_Cam_L_Form(m_chnid, this);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoParam, cam_l_form, &Algo_Cam_L_Form::onGetAlgoParam);
    connect(Singleton::Instance(), &Singleton::sigGetAlarmOut, cam_l_form, &Algo_Cam_L_Form::onGetAlarmOut);
    connect(Singleton::Instance(), &Singleton::sigGetExternalParam, cam_l_form, &Algo_Cam_L_Form::onGetExternalParam);
    cam_l_form->setParent(ui->widget_left);
    cam_l_form->resize(ui->widget_left->width(), ui->widget_left->height());
    cam_l_form->show();
}

void AlgoSetForm::initCamFForm()
{
    ui->lab_theme->setText("高速相机 -算法配置");
    Algo_Cam_H_Form *cam_h_form = new Algo_Cam_H_Form(m_chnid, this);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoParam, cam_h_form, &Algo_Cam_H_Form::onGetAlgoParam);
    connect(Singleton::Instance(), &Singleton::sigGetAlarmOut, cam_h_form, &Algo_Cam_H_Form::onGetAlarmOut);
    connect(Singleton::Instance(), &Singleton::sigGetBeltTearParam, cam_h_form, &Algo_Cam_H_Form::ondrawBeltZone);
    connect(cam_h_form, &Algo_Cam_H_Form::sigaddinfo, this, &AlgoSetForm::onAddInfo);
    connect(cam_h_form, &Algo_Cam_H_Form::sigGetBeltTearZone, this, &AlgoSetForm::onGetBeltTearZone);
    connect(this, &AlgoSetForm::sigdrawBeltZone, cam_h_form, &Algo_Cam_H_Form::ondrawBeltZone);
    cam_h_form->setParent(ui->widget_left);
    cam_h_form->resize(ui->widget_left->width(), ui->widget_left->height());
    cam_h_form->show();
}

void AlgoSetForm::onAddInfo(QString info)
{
    if(ui->widget_right->rects().count() > 0)
    {
        QList<DrawForm::DrawRect> rects = ui->widget_right->rects();
        for(int i = 0; i < rects.count(); ++i)
        {
            if(rects.at(i).selected)
            {
                rects[i].desc = info;
                ui->widget_right->set_detect_Rects(rects);
                ui->widget_right->update();
            }
        }
    }
}

void AlgoSetForm::initARMForm()
{
    ui->lab_theme->setText("分析装置 -算法配置");
    Algo_Arm_Form *cam_arm_form = new Algo_Arm_Form(m_chnid, this);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoParam, cam_arm_form, &Algo_Arm_Form::onGetAlgoParam);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoPeopleCountParam, cam_arm_form, &Algo_Arm_Form::onGetAlgoPeopleCountParam);
    connect(Singleton::Instance(), &Singleton::sigGetPileCoalParam, this, &AlgoSetForm::onGetPileCoalParam);

    connect(Singleton::Instance(), &Singleton::sigPeopleCountMaxChanged, cam_arm_form, &Algo_Arm_Form::onAlgoPeopleCountMaxChanged);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoPeopleCountMaxByTime, cam_arm_form, &Algo_Arm_Form::onGetAlgoPeopleCountMaxByTime);

    connect(Singleton::Instance(), &Singleton::sigGetCorrectZone, this, &AlgoSetForm::onGetCorrectZone);


    connect(cam_arm_form, &Algo_Arm_Form::sigSetPileCoalParam, this, &AlgoSetForm::onSetPileCoalParam);
    connect(cam_arm_form, &Algo_Arm_Form::sigSetCompareZone, this, &AlgoSetForm::onSetCompareZone);

    cam_arm_form->setParent(ui->widget_left);
    cam_arm_form->resize(ui->widget_left->width(), ui->widget_left->height());
    cam_arm_form->show();
}

void AlgoSetForm::initGPUForm()
{
    ui->lab_theme->setText("GPU - 算法配置");
}

void AlgoSetForm::initDZFTForm()
{
    ui->lab_theme->setText("电子封条分析装置 - 算法配置");
    Algo_DZFT_Form  *cam_dzft_form = new Algo_DZFT_Form(m_chnid, this);
    connect(Singleton::Instance(), &Singleton::sigGetAlgoParam, cam_dzft_form, &Algo_DZFT_Form::onGetAlgoParam);
    cam_dzft_form->setParent(ui->widget_left);
    cam_dzft_form->resize(ui->widget_left->width(), ui->widget_left->height());
    cam_dzft_form->show();
}


void AlgoSetForm::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        clickPos = e->pos();
    }
}
void AlgoSetForm::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons()&Qt::LeftButton)
    {
        move(e->pos() + pos() - clickPos);
    }
}

void AlgoSetForm::on_bt_close_clicked()
{
    qDebug() << "close";
    if(ui->btn_RealTimeImage->text() == "关闭预览")
    {
        QMessageBox::critical(nullptr, "警告", "实时预览未关闭");
    }
    else
    {
        this->close();
    }
}

void AlgoSetForm::on_btn_draw_clicked(bool checked)
{
    if(checked)
    {
        show_alarmExtern = false;
        this->on_btn_cleardraw_clicked();
        ui->btn_draw->setText("结束绘制");
        switch (ui->cbx_drawtype->currentIndex())
        {
            case 0:
                ui->widget_right->setDrawMode(DrawForm::DRAW_LINE);
                break;
            case 1:
                ui->widget_right->setDrawMode(DrawForm::DRAW_RECT);
                break;
            case 2:
                ui->widget_right->setDrawMode(DrawForm::DRAW_POLYGON);
                break;
            default:
                break;
        }
    }
    else
    {
        ui->btn_draw->setText("开始绘制");
        if(ui->widget_right->polygons().count() > 0)
        {
            QList<DrawForm::DrawPolygon> polygons = ui->widget_right->polygons();
            for(int i = 0; i < polygons.count(); ++i)
            {
                polygons[i].selected = false;
            }

            ui->widget_right->setPolygons(polygons);
        }
        if(ui->widget_right->lines().count() > 0)
        {
            QList<DrawForm::DrawLine> lines = ui->widget_right->lines();
            for(int i = 0; i < lines.count(); ++i)
            {
                lines[i].selected = false;
            }

            ui->widget_right->setLines(lines);
        }
        if(ui->widget_right->rects().count() > 0)
        {
            QList<DrawForm::DrawRect> rects = ui->widget_right->rects();
            for(int i = 0; i < rects.count(); ++i)
            {
                rects[i].selected = false;
            }

            ui->widget_right->setRects(rects);
        }

        ui->widget_right->setDrawMode(DrawForm::DRAW_NULL);
    }
}

void AlgoSetForm::on_btn_cleardraw_clicked()
{
    show_alarmExtern = false;
    switch (ui->cbx_drawtype->currentIndex())
    {
        case 0:
            ui->widget_right->clearLines();
            break;
        case 1:
            ui->widget_right->clearRects();
            break;
        case 2:
            ui->widget_right->clearPolygons();
            break;
        default:
            break;
    }

}



void AlgoSetForm::on_btn_RealTimeImage_clicked()
{

    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("enable",  ui->btn_RealTimeImage->text() == "打开预览" ? 1 : 0);
    Singleton::Instance()->onSendParam("SetAlarmInfo", body);
    if(ui->btn_RealTimeImage->text() == "打开预览")
    {
        ui->btn_RealTimeImage->setText("关闭预览");
        ui->btn_RealTimeImage->setDisabled(true);
        QTimer::singleShot(2000, this, [&]()
        {
            ui->btn_RealTimeImage->setDisabled(false);
            if(!realmode)
            {
                QMessageBox::information(nullptr, "提示", "预览失败，请重试！", QMessageBox::Yes);
                ui->btn_RealTimeImage->setText("打开预览");
            }
        });
    }
    else
    {
        ui->btn_RealTimeImage->setText("打开预览");
        realmode = 0;
        ui->widget_right->clear();
    }

}



void AlgoSetForm::onAlarmImage(WsBinHeader header, QByteArray image, AlarmExtern alarmExtern)
{

    if(!realmode && ui->btn_RealTimeImage->text() == "打开预览")
    {
        ui->widget_right->clear();
        return;
    }
    if(header.chn != m_chnid)
    {
        return;
    }
    realmode = 1;
    ImageHeader imageHeader;
    int imageHeaderLen = sizeof(ImageHeader);
    memcpy(&imageHeader, header.reserved, imageHeaderLen);
    //if(imageHeader.type == WSIMAGE_TYPE::WSIMAGE_TYPE_COAL)return;
    this->image_bit = image.size();
    ui->widget_right->setImage(image);
    if(show_alarmExtern)
    {
        if(!alarmExtern.boxes.isEmpty())
        {
            QJsonParseError jsonParseError;
            QJsonDocument jsonDoc = QJsonDocument::fromJson(alarmExtern.boxes.toLatin1().data(), &jsonParseError);
            if(!jsonDoc.isEmpty() && (jsonParseError.error == QJsonParseError::NoError))
            {
                if(jsonDoc.isArray())
                {
                    QList<DrawForm::DrawRect> rects;
                    QJsonArray boxesArr = jsonDoc.array();

                    for(auto i = 0; i < boxesArr.count(); ++i)
                    {
                        if(boxesArr.at(i).isObject())
                        {
                            QJsonObject boxObj = boxesArr.at(i).toObject();
                            DrawForm::DrawRect rect;
                            QRectF rectF(boxObj.value("x").toDouble(), boxObj.value("y").toDouble(), boxObj.value("w").toDouble(), boxObj.value("h").toDouble());
                            rect.rect = ui->widget_right->rectF2Rect(rectF);
                            rects.append(rect);
                        }
                    }

                    ui->widget_right->setRects(rects);
                }
            }
        }
        if(!alarmExtern.zones.isEmpty())
        {
            QJsonParseError jsonParseError;
            QJsonDocument jsonDoc = QJsonDocument::fromJson(alarmExtern.zones.toLatin1().data(), &jsonParseError);
            if(!jsonDoc.isEmpty() && (jsonParseError.error == QJsonParseError::NoError))
            {
                if(jsonDoc.isArray())
                {
                    QList<DrawForm::DrawPolygon> polygons;
                    QJsonArray zonesArr = jsonDoc.array();
                    for(auto i = 0; i < zonesArr.count(); ++i)
                    {
                        if(zonesArr.at(i).isArray())
                        {
                            QJsonArray zoneArr = zonesArr.at(i).toArray();
                            DrawForm::DrawPolygon polygon;

                            for(auto j = 0; j < zoneArr.count(); ++j)
                            {
                                QJsonObject zoneObj = zoneArr.at(j).toObject();
                                QPointF pointF(zoneObj.value("x").toDouble(), zoneObj.value("y").toDouble());
                                QPoint point = ui->widget_right->pointF2Point(pointF);
                                polygon.polygon.append(point);
                            }
                            polygons.append(polygon);
                        }
                    }
                    ui->widget_right->setPolygons(polygons);
                }
            }
        }
    }
}

void  AlgoSetForm::onGetZoneParam(QJsonObject obj)
{
//    QJsonParseError receivedJsonParseError;
//    QString message = "{\"body\":{\"lineCnt\":0,\"lines\":null,\"zoneCnt\":1,\"zones\":[[{\"id\":1,\"x\":0.20481927692890167,\"y\":0.42748090624809265},{\"id\":2,\"x\":0.21858863532543182,\"y\":0.91450381278991699},{\"id\":3,\"x\":0.93287438154220581,\"y\":0.96335875988006592},{\"id\":4,\"x\":0.89672976732254028,\"y\":0.32519084215164185}]]},\"code\":200,\"method\":\"GetZoneParam\",\"seq\":14,\"type\":\"response\"}\n";
//    QJsonDocument receivedJsonDoc = QJsonDocument::fromJson(message.toStdString().data(), &receivedJsonParseError);
//    QJsonObject receivedJsonObj = receivedJsonDoc.object();
//    if(receivedJsonObj.contains("type"))
//    {
//        QString type = receivedJsonObj.value("type").toString();
//        if(type == "response")
//        {
//            if(receivedJsonObj.contains("method") && receivedJsonObj.contains("body"))
//            {
//                QString method = receivedJsonObj.value("method").toString();
//                QJsonObject bodyJsonObj = receivedJsonObj.value("body").toObject();
//                if(method == "GetZoneParam")
//                {
//                    obj = bodyJsonObj;
//                }
//            }
//        }
//    }
    if(obj.value("zoneCnt").toInt())
    {
        if(obj.value("zones").isArray())
        {
            QJsonArray zonesArr = obj.value("zones").toArray();
            QList<DrawForm::DrawPolygon> polygons;
            for(int i = 0; i < zonesArr.count(); ++i)
            {
                DrawForm::DrawPolygon polygon;
                if(zonesArr.at(i).isArray())
                {
                    QJsonArray zoneArr = zonesArr.at(i).toArray();
                    for (int j = 0; j < zoneArr.count(); ++j)
                    {
                        if(zoneArr.at(j).isObject())
                        {
                            QJsonObject zoneObj = zoneArr.at(j).toObject();
                            QPointF pointf(zoneObj.value("x").toDouble(), zoneObj.value("y").toDouble());
                            QPoint point = ui->widget_right->pointF2Point(pointf);
                            polygon.polygon.push_back(point);
                        }
                    }
                }
                polygons.push_back(polygon);
            }
            ui->widget_right->set_detect_Polygons(polygons);
        }
    }
    if(obj.value("lineCnt").toInt())
    {
        if(obj.value("lines").isArray())
        {
            QJsonArray linesArr = obj.value("lines").toArray();
            QList<DrawForm::DrawLine> lines;

            for(int i = 0; i < linesArr.count(); ++i)
            {
                DrawForm::DrawLine line;
                if(linesArr.at(i).isArray())
                {
                    QJsonArray lineArr = linesArr.at(i).toArray();
                    QPointF pointf1(lineArr.at(0).toObject().value("x").toDouble(), lineArr.at(0).toObject().value("y").toDouble());
                    QPointF pointf2(lineArr.at(1).toObject().value("x").toDouble(), lineArr.at(1).toObject().value("y").toDouble());
                    QPoint point1 = ui->widget_right->pointF2Point(pointf1);
                    QPoint point2 = ui->widget_right->pointF2Point(pointf2);
                    line.line.setPoints(point1, point2);
                }
                lines.push_back(line);
            }
            ui->widget_right->set_detect_Lines(lines);
        }
    }

    if(obj.value("refBoxCnt").toInt())
    {
        if(obj.value("refBoxs").isArray())
        {
            QList<DrawForm::DrawRect> rects;
            QJsonArray boxesArr = obj.value("refBoxs").toArray();

            for(auto i = 0; i < boxesArr.count(); ++i)
            {
                if(boxesArr.at(i).isObject())
                {
                    QJsonObject boxObj = boxesArr.at(i).toObject();
                    DrawForm::DrawRect rect;
                    rect.desc = QString("%1 %2").arg(boxObj.value("name").toString()).arg(boxObj.value("grade").toString());
                    QRectF rectF(boxObj.value("x").toDouble(), boxObj.value("y").toDouble(), boxObj.value("w").toDouble(), boxObj.value("h").toDouble());
                    rect.rect = ui->widget_right->rectF2Rect(rectF);
                    rects.append(rect);
                }
            }
            ui->widget_right->set_detect_Rects(rects);
        }
    }

}

void AlgoSetForm::on_btn_getZoneParam_clicked()
{
    //2022.3.18 分功能点击查询区域按钮
    if(Algo_Cam_H_Form::mode_beltwidth) //设置皮带宽度模式
    {
        QJsonObject body;
        body.insert("chnId", m_chnid);
        Singleton::Instance()->onSendParam("GetbeltWidth", body);
    }
    // 尾绳检测-矫正图像区域 Added By Wei.Liu 2023-03-27
    else if(Algo_Arm_Form::mode_setCorrectZone)
    {
        QJsonObject body;
        body.insert("chnId", m_chnid);
        Singleton::Instance()->onSendParam("GetCorrectZone", body);
    }
    else
    {
        QJsonObject body;
        body.insert("chnId", m_chnid);
        Singleton::Instance()->onSendParam("GetZoneParam", body);
        show_alarmExtern = true;
    }
}

void AlgoSetForm::on_btn_setZoneParam_clicked()
{
    QJsonObject body;
    QJsonArray ZonesArr;
    int cnt = ui->widget_right->polygons().count();
    for(int i = 0; i < cnt; ++i)
    {

        DrawForm::DrawPolygon polygon = ui->widget_right->polygons().at(i);
        QJsonArray ZoneArr;
        for(int j = 0; j < polygon.polygon.count(); ++j)
        {
            QPointF pointF = ui->widget_right->point2PointF(polygon.polygon.at(j));
            QJsonObject zoneObj;
            //zoneObj.insert("id", j + 1);
            zoneObj.insert("x", pointF.x());
            zoneObj.insert("y", pointF.y());
            ZoneArr.append(zoneObj);
        }
        ZonesArr.append(ZoneArr);
    }

    body.insert("chnId", m_chnid);

    QJsonArray linesArr;
    for(int i = 0; i <  ui->widget_right->lines().count(); ++i)
    {

        DrawForm::DrawLine line = ui->widget_right->lines().at(i);
        QJsonArray lineArr;
        QPointF pointF1 = ui->widget_right->point2PointF(line.line.p1());
        QPointF pointF2 = ui->widget_right->point2PointF(line.line.p2());
        QJsonObject point1Obj;
        QJsonObject point2Obj;
        point1Obj.insert("x", pointF1.x());
        point1Obj.insert("y", pointF1.y());
        lineArr.append(point1Obj);
        point2Obj.insert("x", pointF2.x());
        point2Obj.insert("y", pointF2.y());
        lineArr.append(point2Obj);
        linesArr.append(lineArr);
    }

    QJsonArray boxsArr;
    for(int i = 0; i <  ui->widget_right->rects().count(); ++i)
    {

        DrawForm::DrawRect rect = ui->widget_right->rects().at(i);
        QJsonArray rectArr;
        QRectF rectf = ui->widget_right->rect2RectF(rect.rect);
        QJsonObject rectObj;
        int index = rect.desc.indexOf(" ");
        rectObj.insert("name", rect.desc.mid(0, index)) ;
        rectObj.insert("grade", rect.desc.right(1).toInt());
        rectObj.insert("x", rectf.x());
        rectObj.insert("y", rectf.y());
        rectObj.insert("w", rectf.width());
        rectObj.insert("h", rectf.height());
        boxsArr.append(rectObj);
    }

    if(Algo_Cam_H_Form::mode_beltwidth) //设置皮带宽度模式
    {
        body.insert("beltWidth", linesArr);
        Singleton::Instance()->onSendParam("SetbeltWidth", body);
    }
    // 尾绳检测-矫正图像区域 Added By Wei.Liu 2023-03-27
    else if(Algo_Arm_Form::mode_setCorrectZone)
    {
        body.insert("CorrectZoneCnt", cnt);
        body.insert("CorrectZones", ZonesArr);
        Singleton::Instance()->onSendParam("SetCorrectZone", body);
    }
    else //一般模式
    {
        body.insert("zones", ZonesArr);
        body.insert("zoneCnt", cnt);
        body.insert("lineCnt", ui->widget_right->lines().count());
        body.insert("lines", linesArr);
        body.insert("refBoxCnt", ui->widget_right->rects().count());
        body.insert("refBoxs", boxsArr);
        Singleton::Instance()->onSendParam("SetZoneParam", body);
    }
}

void AlgoSetForm::onGetBeltTearZone()
{
    QJsonObject body;
    if(ui->widget_right->rects().count() > 0)
    {
        DrawForm::DrawRect rect = ui->widget_right->rects().at(0);
        QRectF rectf = ui->widget_right->rect2RectF(rect.rect);
        body.insert("x", rectf.x());
        body.insert("y", rectf.y());
        body.insert("w", rectf.width());
        body.insert("h", rectf.height());
    }
    emit sigdrawBeltZone(body);
    //qDebug() << body;

}

void  AlgoSetForm::onSetPileCoalParam()
{
    QJsonObject body;
    for(int i = 0; i <  ui->widget_right->rects().count(); ++i)
    {
        DrawForm::DrawRect rect = ui->widget_right->rects().at(i);
        QJsonObject body1;
        if(rect.right)
        {
            QRectF rectf = ui->widget_right->rect2RectF(rect.rect);
            body1.insert("x", rectf.x());
            body1.insert("y", rectf.y());
            body1.insert("w", rectf.width());
            body1.insert("h", rectf.height());
            body.insert("detectBox", body1);
        }
        else
        {
            QRectF rectf = ui->widget_right->rect2RectF(rect.rect);
            body1.insert("x", rectf.x());
            body1.insert("y", rectf.y());
            body1.insert("w", rectf.width());
            body1.insert("h", rectf.height());
            body.insert("aoiBox", body1);
        }
    }
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("SetPileCoalParam", body);
}



void AlgoSetForm::onGetPileCoalParam(QJsonObject obj)
{
    QJsonObject aoiBoxObj = obj.value("aoiBox").toObject();
    QJsonObject detectBoxObj = obj.value("detectBox").toObject();
    DrawForm::DrawRect rect, rect_2;
    QList<DrawForm::DrawRect> rects;
    QRectF rectF(aoiBoxObj.value("x").toDouble(), aoiBoxObj.value("y").toDouble(), aoiBoxObj.value("w").toDouble(), aoiBoxObj.value("h").toDouble());
    rect.rect = ui->widget_right->rectF2Rect(rectF);
    QRectF rectF_2(detectBoxObj.value("x").toDouble(), detectBoxObj.value("y").toDouble(), detectBoxObj.value("w").toDouble(), detectBoxObj.value("h").toDouble());
    rect_2.rect = ui->widget_right->rectF2Rect(rectF_2);
    rects.append(rect);
    rects.append(rect_2);
    ui->widget_right->set_detect_Rects(rects);
}

void AlgoSetForm::onGetBeltWidthParam(QJsonObject obj)
{
    if(obj.value("beltWidth").isArray())
    {
        QJsonArray linesArr = obj.value("beltWidth").toArray();
        QList<DrawForm::DrawLine> lines;

        for(int i = 0; i < linesArr.count(); ++i)
        {
            DrawForm::DrawLine line;
            if(linesArr.at(i).isArray())
            {
                QJsonArray lineArr = linesArr.at(i).toArray();
                QPointF pointf1(lineArr.at(0).toObject().value("x").toDouble(), lineArr.at(0).toObject().value("y").toDouble());
                QPointF pointf2(lineArr.at(1).toObject().value("x").toDouble(), lineArr.at(1).toObject().value("y").toDouble());
                QPoint point1 = ui->widget_right->pointF2Point(pointf1);
                QPoint point2 = ui->widget_right->pointF2Point(pointf2);
                line.line.setPoints(point1, point2);
            }
            lines.push_back(line);
        }
        ui->widget_right->set_detect_Lines(lines);
    }
}

void AlgoSetForm::onGetCorrectZone(QJsonObject obj)
{
    if(obj.value("CorrectZoneCnt").toInt())
    {
        if(obj.value("CorrectZones").isArray())
        {
            QJsonArray zonesArr = obj.value("CorrectZones").toArray();
            QList<DrawForm::DrawPolygon> polygons;
            for(int i = 0; i < zonesArr.count(); ++i)
            {
                DrawForm::DrawPolygon polygon;
                if(zonesArr.at(i).isArray())
                {
                    QJsonArray zoneArr = zonesArr.at(i).toArray();
                    for (int j = 0; j < zoneArr.count(); ++j)
                    {
                        if(zoneArr.at(j).isObject())
                        {
                            QJsonObject zoneObj = zoneArr.at(j).toObject();
                            QPointF pointf(zoneObj.value("x").toDouble(), zoneObj.value("y").toDouble());
                            QPoint point = ui->widget_right->pointF2Point(pointf);
                            polygon.polygon.push_back(point);
                        }
                    }
                }
                polygons.push_back(polygon);
            }
            ui->widget_right->set_detect_Polygons(polygons);
        }
    }
}

void AlgoSetForm::onSetCompareZone(int width)
{

    if(width == 0)
    {
        DrawForm::DrawRect rect;
        ui->widget_right->set_compare_rect(rect);
        ui->widget_right->update();
        return;
    }
#if TEST
    double   scale = (width * width) / (1920 * 1080.0);
#else
    double   scale = (width * width) / (image_bit / 24.0);
#endif
    double area = ui->widget_right->width() * ui->widget_right->height() * scale;
    //qDebug() << scale << area << ui->widget_right->width() << ui->widget_right->height();
    double sq_scale = ui->widget_right->width() / (ui->widget_right->height() + 0.0);
    //qDebug() << sq_scale;
    double rect_height = sqrt(1 / sq_scale * area) ;
    double rect_width = rect_height * sq_scale ;
    qDebug() << "compare rect:" << rect_width << "*" << rect_height;
    DrawForm::DrawRect rect;
    QRectF rectF(0.3, 0.3, rect_width / ui->widget_right->width(), rect_height / ui->widget_right->height());
    rect.rect = ui->widget_right->rectF2Rect(rectF);
    rect.desc = "对比框";
    ui->widget_right->set_compare_rect(rect);
    ui->widget_right->update();


}




